﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarketOnlineBUS;
using MarketOnlineDTO;

public partial class cms_ManageProductDetail : System.Web.UI.Page
{
    private SanPhamDTO sp = new SanPhamDTO();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadDanhSachLoaiSP();
        LoadDanhSachTinhTrang();
        LoadDanhSachDonVi();
        string catid = Request.QueryString["catid"].ToString();
        int masp = Convert.ToInt32(catid);
        if (masp != 0)
        {
            LayThongTinSanPham(masp);
        }
    }

    void LayThongTinSanPham(int masp)
    {        
        sp = new SanPhamBUS().LaySanPham(masp);        
        txtTenSanPham.Text = sp.TenSP.ToString();
        txtDonGia.Text = sp.DonGia.ToString();
        txtMoTa.Text = sp.MoTaChiTiet.ToString();
        txtSoLuong.Text = sp.SoLuongTon.ToString();
        ddlDonVi.DataValueField = sp.MaDonViTinh.ToString();
        ddlTinhTrang.DataValueField = sp.MaTinhTrangSP.ToString();
        ddlLoaiSP.DataValueField = sp.MaLoaiSanPham.ToString();
        lblHinh.Text = sp.HinhAnh;
        imgHinh.ImageUrl = "~/images/product/" + lblHinh.Text;
        Dispose();
    }

    void LoadDanhSachTinhTrang()
    {
        List<TinhTrangSanPhamDTO> lst = new List<TinhTrangSanPhamDTO>();
        lst = new TinhTrangSanPhamBUS().LayDanhSachTinhTrangSanPham();
        ddlTinhTrang.DataSource = lst;
        ddlTinhTrang.DataValueField = "MaTinhTrangSP";
        ddlTinhTrang.DataTextField = "TenTinhTrangSP";
        ddlTinhTrang.DataBind();
        Dispose();
    }

    void LoadDanhSachDonVi()
    {
        List<DonViTinhDTO> lst = new List<DonViTinhDTO>();
        lst = new DonViTinhBUS().LayDanhSachDonViTinh();
        ddlDonVi.DataSource = lst;
        ddlDonVi.DataValueField = "MaDonViTinh";
        ddlDonVi.DataTextField = "TenDonViTinh";
        ddlDonVi.DataBind();
        Dispose();
    }

    void LoadDanhSachLoaiSP()
    {
        List<LoaiSanPhamDTO> lst = new List<LoaiSanPhamDTO>();
        lst = new LoaiSanPhamBUS().LayDanhSachLoaiSanPham();
        ddlLoaiSP.DataSource = lst;
        ddlLoaiSP.DataValueField = "MaLoaiSP";
        ddlLoaiSP.DataTextField = "TenLoaiSP";
        ddlLoaiSP.DataBind();
        Dispose();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string url = "ManageProduct.aspx";
        Response.Redirect(url);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string catid = Request.QueryString["catid"].ToString();
        int dk = Convert.ToInt32(catid);
        if (dk != 0)
        {
            sp.TenSP = Request.Form[4].ToString();
            sp.DonGia = Convert.ToDouble(Request.Form[5].ToString());
            sp.MoTaChiTiet = Request.Form[6].ToString();
            sp.SoLuongTon = Convert.ToDouble(Request.Form[10].ToString());
            sp.MaDonViTinh = int.Parse(Request.Form[9].ToString());
            sp.MaLoaiSanPham = int.Parse(Request.Form[8].ToString());
            sp.MaTinhTrangSP = int.Parse(Request.Form[7].ToString());
            new SanPhamBUS().CapNhatSanPham(sp);
            string url = "ManageProduct.aspx";
            Response.Redirect(url);
        }
        if (dk == 0)
        {
            sp.TenSP = Request.Form[4].ToString();
            sp.DonGia = Convert.ToDouble(Request.Form[5].ToString());
            sp.MoTaChiTiet = Request.Form[6].ToString();
            sp.SoLuongTon = Convert.ToDouble(Request.Form[10].ToString());
            sp.MaDonViTinh = int.Parse(Request.Form[9].ToString());
            sp.MaLoaiSanPham = int.Parse(Request.Form[8].ToString());
            sp.MaTinhTrangSP = int.Parse(Request.Form[7].ToString());
            new SanPhamBUS().ThemSanPham(sp);
            string url = "ManageProduct.aspx";
            Response.Redirect(url);
        }
    }
}